package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.enums.CheckStatusEnum;
import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.dto.PositionDto;
import com.qinggeng.project.api.dto.ResumeDto;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.JobUser;
import com.qinggeng.project.system.domain.Resume;
import com.qinggeng.project.system.mapper.ResumeMapper;
import com.qinggeng.project.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class ResumeServiceImpl implements IResumeService {


    @Resource
    private ResumeMapper resumeMapper;
    @Autowired
    private IJobUserService jobUserService;
    @Autowired
    private IPositionService positionService;
    @Autowired
    private IPositionClassifyService positionClassifyService;
    @Autowired
    private IPositionNameService positionNameService;
    @Override
    public boolean insertResume(Resume resume) {
        //根据用户id查询简历
        Resume oldResume=resumeMapper.getResumeByJobUserId(resume.getJobUserId());
        if(oldResume!=null){
            throw new CustomException("请勿重复发布简历！", ShowTypeEnum.用户.getValue());
        }
        resume.setApplyPosition(positionNameService.checkPositionName(resume.getPositionCode()).getName());
        resume.setStatus(CheckStatusEnum.审核中.getValue());
        return resumeMapper.insertResume(resume)>0;
    }

    @Override
    public boolean deleteResumeById(Long id) {
        this.getResumeByIdAndUserId(id);
        return resumeMapper.deleteResumeById(id)>0;
    }

    @Override
    public int deleteResumeByIds(Long[] ids) {
        return resumeMapper.deleteResumeByIds(ids);
    }

    @Override
    public Resume getResumeById(Long id){
        return resumeMapper.getResumeById(id);
    }

    @Override
    public Resume getResumeByUserId(Long userId) {
        Resume resume=resumeMapper.getResumeByUserId(userId);
        if(null==resume){
            throw new CustomException("简历不存在",ShowTypeEnum.用户.getValue());
        }
        return resume;
    }
    @Override
    public Resume getResumeByUserId() {
        return resumeMapper.getResumeByUserId(ApiUtils.getUserId());
    }

    @Override
    public Resume getResumeByIdAndUserId(Long id) {
        Resume resume=resumeMapper.getResumeByIdAndUserId(id,ApiUtils.getUserId());
        if(null==resume){
            throw new CustomException("简历不存在或没操作权限！",ShowTypeEnum.用户.getValue());
        }
        return resume;
    }

    @Override
    public List<Resume> listResume(Resume where) {
        return resumeMapper.listResume(where);
    }

    @Override
    public boolean updateResume(Resume resume) {
        resume.setApplyPosition(positionNameService.checkPositionName(resume.getPositionCode()).getName());
        resume.setStatus(CheckStatusEnum.审核中.getValue());
        return resumeMapper.updateResume(resume)>0;
    }

    public boolean checkResume(Resume resume) {
        return resumeMapper.updateResume(resume)>0;
    }

    @Override
    public List<ResumeDto> listResumeDto(Resume where) {
        where.setStatus(CheckStatusEnum.已通过.getValue());
        return resumeMapper.listResumeDto(where);
    }

    public List<ResumeDto> listCollectResumeDto(){
        return resumeMapper.listCollectResumeDto(ApiUtils.getUserId());
    }

    @Override
    public List<ResumeDto> listBrowseResumeDto() {
        return resumeMapper.listBrowseResumeDto(ApiUtils.getUserId());
    }

    @Override
    public ResumeDto getResumeDtoById(Long id) {
        ResumeDto resumeDto=resumeMapper.getResumeDtoById(id);
        if(null==resumeDto){
            throw new CustomException("简历不存在或已被删除", ShowTypeEnum.用户.getValue());
        }
        resumeMapper.updateResumeBrowseCount(id);
        return resumeDto;
    }

    @Override
    public String getResumePhoneNumber(Long id) {
        String contact=resumeMapper.getResumePhoneNumber(id);
        if(StringUtils.isEmpty(contact)){
            throw new CustomException("您要查看的联系方式不存在",ShowTypeEnum.用户.getValue());
        }
        return contact;
    }


    public Integer getResumeBrowseCountById(Long id){
        Resume resume=this.getResumeById(id);
        if(null==resume||null==resume.getBrowseCount()){
            return 0;
        }
        return resume.getBrowseCount();
    }

}
